package model;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tetris extends JFrame implements ActionListener {

	private GamePanel gamePanel = new GamePanel();
	private JButton startButton = new JButton("Start");
	private JButton quitButton = new JButton("Quit");
	private JButton pauseButton = new JButton("Pause");
	private boolean running = false;
	private boolean paused = false;
	private int fps = 25;
	private int frameCount = 0;
	
	public Tetris() {
		super("Tetris");
		Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
		JPanel p = new JPanel();
		p.setLayout(new GridLayout(1,2));
		p.add(startButton);
		p.add(pauseButton);
		p.add(quitButton);
		cp.add(gamePanel, BorderLayout.CENTER);
		cp.add(paramString(), BorderLayout.SOUTH);
		setSize(500, 500);
		
		startButton.addActionListener(this);
		quitButton.addActionListener(this);
		pauseButton.addActionListener(this);
	}
	
	public static void main(String[] args) {
		Tetris tet = new Tetris();
		tet.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		Object s = e.getSource();
		if (s == startButton) {
			running = !running;
			if (running) {
				startButton.setText("Stop");
				runGameLoop();
			}
			else {
				startButton.setText("Start");
			}
		}
		else if (s == pauseButton) {
			paused = !paused;
			if (paused) {
				pauseButton.setText("Resume");
			}
			else {
				pauseButton.setText("Pause");
			}
		}
		else if (s == quitButton) {
			System.exit(0);
		}
	}
	
	public void runGameLoop() {
//		final double GAME_HERTZ = 30.0;
//		final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
//		final int MAX_UPDATES_BEFORE_RENDER = 1;
//		double lastUpdateTime = System.nanoTime();
//		double lastRenderTime = System.nanoTime();
//		
//		final double TARGET_FPS = 60;
//		final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;;
//		
//		int lastSecondTime = (int) (lastUpdateTime / 1000000000);
		
		while (running) {
//			double now = System.nanoTime();
//			int updateCount = 0;
//			if (!paused) {
//				 //Do as many game updates as we need to, potentially playing catchup.
//	            while( now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER )
//	            {
//	               updateGame();
//	               lastUpdateTime += TIME_BETWEEN_UPDATES;
//	               updateCount++;
//	            }
//	   
//	            //If for some reason an update takes forever, we don't want to do an insane number of catchups.
//	            //If you were doing some sort of game that needed to keep EXACT time, you would get rid of this.
//	            if ( now - lastUpdateTime > TIME_BETWEEN_UPDATES)
//	            {
//	               lastUpdateTime = now - TIME_BETWEEN_UPDATES;
//	            }
//	         
//	            //Render. To do so, we need to calculate interpolation for a smooth render.
//	            float interpolation = Math.min(1.0f, (float) ((now - lastUpdateTime) / TIME_BETWEEN_UPDATES) );
//	            drawGame(interpolation);
//	            lastRenderTime = now;
//	         
//	            //Update the frames we got.
//	            int thisSecond = (int) (lastUpdateTime / 1000000000);
//	            if (thisSecond > lastSecondTime)
//	            {
//	               System.out.println("NEW SECOND " + thisSecond + " " + frameCount);
//	               fps = frameCount;
//	               frameCount = 0;
//	               lastSecondTime = thisSecond;
//	            }
//	         
//	            //Yield until it has been at least the target time between renders. This saves the CPU from hogging.
//	            while ( now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS && now - lastUpdateTime < TIME_BETWEEN_UPDATES)
//	            {
//	               Thread.yield();
//	            
//	               //This stops the app from consuming all your CPU. It makes this slightly less accurate, but is worth it.
//	               //You can remove this line and it will still work (better), your CPU just climbs on certain OSes.
//	               //FYI on some OS's this can cause pretty bad stuttering. Scroll down and have a look at different peoples' solutions to this.
//	               try {Thread.sleep(1);} catch(Exception e) {} 
//	            
//	               now = System.nanoTime();
//	            }				
//			}
			updateGame();
		}
	}
	
	public void updateGame() {
		gamePanel.update();
	}
	
	public void drawGame(float interpolation) {
		gamePanel.setInterpolation(interpolation);
		gamePanel.repaint();
	}
}